package com.example.xiaozu.activity;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import com.example.xiaozu.Fragment.HeadlinesFragment;

import com.example.xiaozu.Fragment.MallFragment;
import com.example.xiaozu.Fragment.MeFragment;
import com.example.xiaozu.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
        bottomNav.setOnNavigationItemSelectedListener(navListener);

        // 默认加载头条Fragment
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, new HeadlinesFragment())
                .commit();
    }

    private final BottomNavigationView.OnNavigationItemSelectedListener navListener =
            item -> {
                Fragment selectedFragment = null;
                int id = item.getItemId();

                if (id == R.id.nav_headlines) {
                    selectedFragment = new HeadlinesFragment();
                } else if (id == R.id.nav_mall) {
                    selectedFragment = new MallFragment();
                } else if (id == R.id.nav_me) {
                    selectedFragment = new MeFragment();
                }

                if (selectedFragment != null) {
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.fragment_container, selectedFragment)
                            .commit();
                    return true;
                }
                return false;
            };
}

